package com.ky.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ky.tms.entity.TmsCarType;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.mapper.TmsCarTypeMapper;
import com.ky.tms.service.ITmsCarTypeService;
import com.ky.tms.util.RequestContextHolderUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2020-06-05
 */
@Service
public class TmsCarTypeServiceImpl extends ServiceImpl<TmsCarTypeMapper, TmsCarType> implements ITmsCarTypeService {

    @Autowired
    private TmsCarTypeMapper tmsCarTypeMapper;

    @Override
    public IPage<TmsCarType> page(TmsCarType tmsCarType, Long limit, Long pages) {
        IPage<TmsCarType> page = new Page(pages, limit);
        QueryWrapper<TmsCarType> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(tmsCarType.getCustomerName())){
            wrapper.like("customer_name", tmsCarType.getCustomerName());
        }
        wrapper.eq("is_valid", 1);
        wrapper.orderByDesc("create_time");
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public void add(TmsCarType tmsCarType) throws PredictableException {
        String userName = RequestContextHolderUtil.getUserName();
        tmsCarType.setIsLock(0);
        tmsCarType.setIsValid(1);
        tmsCarType.setCreateTime(LocalDateTime.now());
        tmsCarType.setCreateUser(userName);
        tmsCarTypeMapper.insert(tmsCarType);
    }

    @Override
    public void delete(List<Long> ids) {
        String userName = RequestContextHolderUtil.getUserName();
        tmsCarTypeMapper.deleteByIds(userName, LocalDateTime.now(), ids);
    }

    @Override
    public void update(TmsCarType tmsCarType) throws PredictableException {
        String userName = RequestContextHolderUtil.getUserName();
        tmsCarType.setUpdateTime(LocalDateTime.now());
        tmsCarType.setUpdateUser(userName);
        tmsCarTypeMapper.updateById(tmsCarType);
    }

    @Override
    public List<Map<String, Object>> selectList(Long customerId) {
        return tmsCarTypeMapper.selectOption(customerId);
    }
}
